package org.zz.tool;

import java.util.Arrays;

public class ImgFormatTrans {
    public ImgFormatTrans() {
    }

    static int FPICryptMAC(byte[] psKey, byte[] psRandom, byte[] psInput, int nInLen, byte[] psOutput, int[] pnOutLen) {
        byte[] keyData = new byte[16];
        byte[] random = new byte[8];
        byte[] input = new byte[nInLen];
        byte[] output = new byte[8];
        byte[] buf = new byte[8];
        Arrays.fill(keyData, (byte) 0x00);
        Arrays.fill(random, (byte) 0x00);
        Arrays.fill(input, (byte) 0x00);
        Arrays.fill(output, (byte) 0x00);
        System.arraycopy(psKey, 0, keyData, 0, 16);
        System.arraycopy(psRandom, 0, random, 0, 8);
        System.arraycopy(psInput, 0, input, 0, nInLen);
        int blockCount = nInLen / 8;
        int lastBlockSize = nInLen % 8;
        if(lastBlockSize > 0) {
            ++blockCount;
        } else {
            lastBlockSize = 8;
        }

        System.arraycopy(random, 0, output, 0, 8);

        int j;
        for(int i = 0; i < blockCount; ++i) {
            if(i < blockCount - 1) {
                System.arraycopy(input, 8 * i, buf, 0, 8);
            } else {
                Arrays.fill(buf, (byte) 0x00);
                System.arraycopy(input, 8 * i, buf, 0, lastBlockSize);
            }

            for(j = 0; j < 8; ++j) {
                output[j] ^= buf[j];
            }
        }

        for(j = 0; j < 8; ++j) {
            output[j] ^= keyData[j];
        }

        for(j = 0; j < 8; ++j) {
            output[j] ^= keyData[j + 8];
        }

        System.arraycopy(output, 0, psOutput, 0, 8);
        pnOutLen[0] = 8;
        return -1;
    }

    static void FillImgHead(byte[] imgData, int iDpi, int iWidth, int iHeight) {
        Arrays.fill(imgData, (byte) 0x00);
        byte nLength = 0;
        System.arraycopy("FIR".getBytes(), 0, imgData, 0, 3);
        int var5 = nLength + 4;
        System.arraycopy("010".getBytes(), 0, imgData, var5, 3);
        var5 += 4;
        imgData[var5] = (byte)((iWidth * iHeight + 46) % 256);
        imgData[var5 + 1] = (byte)((iWidth * iHeight + 46) / 256);
        imgData[var5 + 4] = 0;
        imgData[var5 + 5] = 0;
        var5 += 6;
        imgData[var5] = 1;
        imgData[var5 + 1] = 2;
        var5 += 2;
        imgData[var5] = 1;
        imgData[var5 + 1] = 0;
        var5 += 2;
        imgData[var5] = 1;
        ++var5;
        imgData[var5] = 1;
        ++var5;
        imgData[var5] = (byte)(iDpi / 256);
        imgData[var5 + 1] = (byte)(iDpi % 256);
        var5 += 2;
        imgData[var5] = (byte)(iDpi / 256);
        imgData[var5 + 1] = (byte)(iDpi % 256);
        var5 += 2;
        imgData[var5] = (byte)(iDpi / 256);
        imgData[var5 + 1] = (byte)(iDpi % 256);
        var5 += 2;
        imgData[var5] = (byte)(iDpi / 256);
        imgData[var5 + 1] = (byte)(iDpi % 256);
        var5 += 2;
        imgData[var5] = 8;
        ++var5;
        imgData[var5] = 0;
        ++var5;
        imgData[var5] = 0;
        imgData[var5 + 1] = 0;
        var5 += 2;
        imgData[var5] = (byte)(iWidth * iHeight % 256);
        imgData[var5 + 1] = (byte)(iWidth * iHeight / 256);
        var5 += 4;
        imgData[var5] = 0;
        ++var5;
        imgData[var5] = 1;
        ++var5;
        imgData[var5] = 1;
        ++var5;
        imgData[var5] = 87;
        ++var5;
        imgData[var5] = 0;
        ++var5;
        imgData[var5] = (byte)(iWidth / 256);
        imgData[var5 + 1] = (byte)(iWidth % 256);
        var5 += 2;
        imgData[var5] = (byte)(iHeight / 256);
        imgData[var5 + 1] = (byte)(iHeight % 256);
        var5 += 2;
        ++var5;
    }

    static void FillImgHeadEx(byte[] imgData, int iDpi, int iWidth, int iHeight) {
        Arrays.fill(imgData, (byte) 0x00);
        byte nLength = 0;
        System.arraycopy("FIR".getBytes(), 0, imgData, 0, "FIR".length());
        int var6 = nLength + 4;
        System.arraycopy("010".getBytes(), 0, imgData, var6, "010".length());
        var6 += 4;
        imgData[var6] = (byte)((iWidth * iHeight + 46) % 256);
        imgData[var6 + 1] = (byte)((iWidth * iHeight + 46) / 256);

        int i;
        for(i = var6 + 2; i < 2; ++i) {
            imgData[var6 + 2 + i] = 0;
        }

        imgData[var6 + 4] = 0;
        imgData[var6 + 5] = 0;
        var6 += 6;
        imgData[var6] = 1;
        imgData[var6 + 1] = 2;
        var6 += 2;
        imgData[var6] = 1;
        imgData[var6 + 1] = 0;
        var6 += 2;
        imgData[var6] = 1;
        ++var6;
        imgData[var6] = 1;
        ++var6;
        imgData[var6] = (byte)(iDpi / 256);
        imgData[var6 + 1] = (byte)(iDpi % 256);
        var6 += 2;
        imgData[var6] = (byte)(iDpi / 256);
        imgData[var6 + 1] = (byte)(iDpi % 256);
        var6 += 2;
        imgData[var6] = (byte)(iDpi / 256);
        imgData[var6 + 1] = (byte)(iDpi % 256);
        var6 += 2;
        imgData[var6] = (byte)(iDpi / 256);
        imgData[var6 + 1] = (byte)(iDpi % 256);
        var6 += 2;
        imgData[var6] = 8;
        ++var6;
        imgData[var6] = 0;
        ++var6;
        imgData[var6] = 0;
        imgData[var6 + 1] = 0;
        var6 += 2;

        for(i = var6; i < 2; ++i) {
            imgData[var6 + i] = 0;
        }

        imgData[var6] = (byte)(iWidth * iHeight % 256);
        imgData[var6 + 1] = (byte)(iWidth * iHeight / 256);

        for(i = var6 + 2; i < 2; ++i) {
            imgData[var6 + 2 + i] = 0;
        }

        var6 += 4;
        imgData[var6] = 0;
        ++var6;
        imgData[var6] = 1;
        ++var6;
        imgData[var6] = 1;
        ++var6;
        imgData[var6] = 87;
        ++var6;
        imgData[var6] = 0;
        ++var6;
        imgData[var6] = (byte)(iWidth / 256);
        imgData[var6 + 1] = (byte)(iWidth % 256);
        var6 += 2;
        imgData[var6] = (byte)(iHeight / 256);
        imgData[var6 + 1] = (byte)(iHeight % 256);
        var6 += 2;
        ++var6;
    }

    public static int ImgToIso(byte[] imageBuf, int iDpi, int iWidth, int iHeight, byte[] isoImageBuf) {
        String strKey = "MIAXIS1234567890";
        byte[] psKey = strKey.getBytes();
        String strRandom = "12345678";
        byte[] psRandom = strRandom.getBytes();
        byte[] tmpHead = new byte[47];
        byte[] tmpMac = new byte[9];
        int[] iMacLen = new int[1];
        int iImageSize = iWidth * iHeight;
        FillImgHeadEx(tmpHead, iDpi, iWidth, iHeight);
        System.arraycopy(tmpHead, 0, isoImageBuf, 0, 46);
        System.arraycopy(imageBuf, 0, isoImageBuf, 46, iImageSize);
        FPICryptMAC(psKey, psRandom, isoImageBuf, iImageSize + 46, tmpMac, iMacLen);
        System.arraycopy(tmpMac, 0, isoImageBuf, iImageSize + 46, 8);
        return 0;
    }
}
